package com.eastfair.security.handler;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPatch;
import com.eastfair.annotation.annotation.cache.Cache;
import com.eastfair.annotation.annotation.cache.Magic;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.context.StaticContextUtil;
import com.eastfair.core.handler.DataScopeHandler;
import com.eastfair.core.model.DataIsolationModel;
import com.eastfair.core.model.SystemTagEnum;
import com.eastfair.model.DataIsolationVo;
import com.eastfair.projectcore.api.ProjectCoreServiceFeign;
import com.eastfair.projectcore.dto.DataIsolationDTO;
import com.eastfair.projectcore.enumeration.DataIsolationDataScopeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class DataIsolactionHandler {
    @Autowired
    ProjectCoreServiceFeign projectCoreServiceFeign;

    //    @Cache(key = "#hash(#args)")
    public List<DataIsolationVo> queryDataIsolaction(Long projectId) {
        //加载系统数据信息
        DataIsolationDTO dataIsolationDTO = new DataIsolationDTO();
        dataIsolationDTO.setProjectId(projectId);
        R<List<DataIsolationVo>> listR = projectCoreServiceFeign.queryDataIsolaction(dataIsolationDTO);
        if (listR.getIsSuccess()) {
            return listR.getData();
        }
        return null;
    }

    public Long switchSystemId() {
        //本系统隔离级别,null代表基础服务，跟随调用方定义级别
        if (DataIsolationModel.getSelfTag() != null && SystemTagEnum.COMMON.equals(DataIsolationModel.getSelfTag())) {
            return null;
        }
        String staticProjectSystemDataScope = StaticContextUtil.getStaticProjectSystemDataScope();
        List<Map> mapList = JSONArray.parseArray(staticProjectSystemDataScope, Map.class);
        if (mapList == null || mapList.isEmpty()) {
            return null;
        }
        return DataScopeHandler.findSystemId(staticProjectSystemDataScope);
    }

    public Long assmableSystemId(String systemScene) {

        String dataListStr = StaticContextUtil.getDataIsolcationList();
//        log.info("systemScene is {} ， dataListStr is {}", systemScene, dataListStr);
        if (StringUtils.isBlank(dataListStr)) {
            return null;
        }
        List<DataIsolationVo> dataIsolationVoList = JSONArray.parseArray(dataListStr, DataIsolationVo.class);
        if (CollectionUtil.isEmpty(dataIsolationVoList)) {
            return null;
        }
        for (DataIsolationVo dataIsolationVo : dataIsolationVoList) {
            if (dataIsolationVo.getSystemScene().equals(systemScene) && dataIsolationVo.getProjectId().equals(ContextUtil.getProjectId())) {
                return dataIsolationVo.getId();
            }
        }
        return null;
    }
}
